package com.iwhalecloud.uncc.properties;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Component("unccDsProperties")
@ConfigurationProperties(prefix = "uncc")
public class UnccDsProperties {

    private final List<UnccDsConfig> datasource = new ArrayList<UnccDsConfig>();
    private List<UnccDsConfig> bizDatasource = new ArrayList<UnccDsConfig>();
    public List<UnccDsConfig> getDatasource() {
        return datasource;
    }
    public List<UnccDsConfig> getBizDatasource() {
        return bizDatasource;
    }
    /***
     * 获取业务数据库，去掉默认数据库和性能数据库
     * @return
     */
    @PostConstruct
    public List<UnccDsConfig> setBizDatasource() {
        if(CollectionUtils.isEmpty(bizDatasource)) {
            bizDatasource= datasource.stream().filter(i -> !"nms".equals(i.getCode()) && !"default".equals(i.getCode()))
                    .collect(Collectors.toList());
        }
        return bizDatasource;
    }

    public static class UnccDsConfig {

        private String code;
        private Map<String, String> properties;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public Map<String, String> getProperties() {
            return properties;
        }

        public void setProperties(Map<String, String> properties) {
            this.properties = properties;
        }
    }
}
